package com.siqi.hr.common.redis;

import com.siqi.hr.common.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 连接redis服务线程池工具类
 * @author  xiaogq
 */
public class RedisPoolManager {

  static Logger log = LoggerFactory.getLogger(RedisPoolManager.class);

  private static JedisPool pool;
  
  static{
      if (pool == null) {
          JedisPoolConfig config = new JedisPoolConfig();
          config.setMaxActive(Constants.REDIS_MAXACTIVE);
          config.setMaxIdle(Constants.REDIS_MAXIDLE);
          config.setMaxWait(Constants.REDIS_MAXWAIT);
          config.setTestOnBorrow(Constants.REDIS_TESTONBORROW);
          pool = new JedisPool(config, Constants.REDIS_IP, Constants.REDIS_PORT,Constants.REDIS_DELAY);
      }
  }
  
  /**
   * Get Jedis resource from the pool
   * @return
   */
  public static Jedis createInstance() {
    Jedis jedis = pool.getResource();
    return jedis;
  }
  
  /**
   * Return the resource to pool
   * @param jedis
   */
  public static void returnResource(Jedis jedis) {
    pool.returnResource(jedis);
  }
}